﻿using System;
using System.Collections.Generic;
using Color = System.Drawing.Color;
using SwinGame;
using System.Diagnostics;

namespace Jumpman.src
{
    public class Stage
    {


        private Music backgroundMusic;

        public Music BackgroundMusic
        {
            get { return backgroundMusic; }
            set { backgroundMusic = value; }
        }

        private IAction applyGravity = new GravityAction();
        private IAction checkCollision = new CollisionAction();

        private Bitmap backgroundImage;

        public Bitmap BackgroundImage
        {
            set { backgroundImage = value; }
        }

        private List<IDrawable> listOfDrawable = new List<IDrawable>();

        public List<IDrawable> ListOfDrawable
        {
            get { return listOfDrawable; }
            set { listOfDrawable = value; }
        }

        private List<Blocks> listOfBlocks = new List<Blocks>();

        public List<Blocks> ListOfBlocks
        {
            get { return listOfBlocks; }
            set { listOfBlocks = value; }
        }


        private List<GameElement> listOfGameElements = new List<GameElement>();

        public List<GameElement> ListOfGameElements
        {
            get { return listOfGameElements; }
            set { listOfGameElements = value; }
        }

        private List<IUpdatable> listofUpdatables = new List<IUpdatable>();

        public List<IUpdatable> ListofUpdatables
        {
            get { return listofUpdatables; }
            set { listofUpdatables = value; }
        }





        //THIS IS WHERE UPDATE STARTS!!!
        public void Update(GameController gc)
        {
            if (!Music.IsPlaying())
            {
                backgroundMusic.Play();
            }


                
                
            if (listOfGameElements.Count != 0 )
            {
                applyGravity.DoAction(gc);
                checkCollision.DoAction(gc);

            }
                
            

            foreach (IUpdatable u in listofUpdatables)
            {
                u.Update(gc);
               
            }


        }

        public void Draw(GameController gc)
        {
            const int WIDTH = 3822;
            if (gc.Player!= null)
            {
                float mx = gc.Player.ElementSprite.X - 200f; //200 is mario's offsetx pos from left edge. 
                int widthMultiplier = (int)(mx / WIDTH);
                if ((Core.ScreenWidth() + mx) > WIDTH)
                {
                    backgroundImage.Draw(((widthMultiplier + 1) * WIDTH), 0f);
                }
                backgroundImage.Draw((widthMultiplier * WIDTH), 0f);
            }
            else
                backgroundImage.Draw(0f, 0f);

            foreach (IDrawable d in listOfDrawable)
            {
                d.Draw();
            }
            //stageMap.Draw();


            foreach (Blocks b in listOfBlocks)
            {
                b.Draw();
            }
        }

    }
}
